import _ from "lodash";

// 获取两个时间的相差 年月日
export function dayCha(startime, endtime) {
  startime = startime.replace(new RegExp("-", "gm"), "/");//转换-变为/
  endtime = endtime.replace(new RegExp("-", "gm"), "/");
  var minTime = new Date(startime).getTime() > new Date(endtime).getTime() ? endtime : startime
  var maxTime = new Date(startime).getTime() > new Date(endtime).getTime() ? startime : endtime

  var start = new Date(minTime);
  var startYear = start.getFullYear(); //开始年份
  var startMonth = start.getMonth() + 1;//开始月份


  var end = new Date(maxTime);
  var endYear = end.getFullYear(); //结束年份
  var endMonth = end.getMonth() + 1;//结束月份

  //  年
  var mideleYear = new Date(minTime)
  mideleYear.setFullYear(endYear)
  var years = Math.abs(mideleYear.getFullYear() - start.getFullYear());
  if (mideleYear.getTime() > end.getTime()) {
    years--
  }

  // 月
  var mideleMonthDay = new Date(minTime)
  mideleMonthDay.setFullYear(endYear)
  mideleMonthDay.setMonth(endMonth - 1)
  var months = (endYear * 12 + endMonth) - (startYear * 12 + startMonth) - years * 12
  if (mideleMonthDay.getTime() > end.getTime()) {
    months--
  }

  // 日
  var day = (Math.floor((end.getTime() - start.getTime()) / 24 / 60 / 60 / 1000))
  if (end.getTime() > mideleMonthDay.getTime()) {
    day = (Math.floor((end.getTime() - mideleMonthDay.getTime()) / 24 / 60 / 60 / 1000)) - day
  } else {
    let prevMonthDay = new Date(mideleMonthDay)
    prevMonthDay.setMonth(endMonth - 2)
    day = (Math.floor((end.getTime() - prevMonthDay.getTime()) / 24 / 60 / 60 / 1000))
  }

  var chaTime = end.getTime() - start.getTime()

  var chaDay = Math.floor(chaTime / 24 / 60 / 60 / 1000)

  var hour = Math.floor(chaTime / 60 / 60 / 1000) - chaDay * 24

  var min = Math.floor(chaTime / 60 / 1000) - chaDay * 24 * 60 - hour * 60

  var sec = Math.floor(chaTime / 1000) - chaDay * 24 * 60 * 60 - hour * 60 * 60 - min * 60

  var yearsSpaced = years + '年' + months + '月'
  var daySpaced = day + '天'
  var hourSpaced = hour + '时:' + min + '分:' + sec + '秒'
  var timeSpaced = {
    yearsSpaced,
    daySpaced
  }
  return timeSpaced
}
// 获取当前的时间 2023-20-8
export function newDateFn() {
  const now = new Date();

  const year = now.getFullYear();
  const month = ('0' + (now.getMonth() + 1)).slice(-2);
  const day = ('0' + now.getDate()).slice(-2);
  const hours = ('0' + now.getHours()).slice(-2);
  const minutes = ('0' + now.getMinutes()).slice(-2);
  const seconds = ('0' + now.getSeconds()).slice(-2);

  let formattedTime = year + '-' + month + '-' + day;
  return formattedTime
}

// 计算两个时间之间相差的天数——入参是new Date(‘2023-01-13 10:14:30’)这样的格式
export function getDiffDay(date_1, date_2) {
  let totalDays, diffDate
  let myDate_1 = Date.parse(date_1)
  let myDate_2 = Date.parse(date_2)
  diffDate = myDate_1 - myDate_2
  totalDays = Math.floor(diffDate / (1000 * 3600 * 24))
  if (totalDays > 0) {
    totalDays = totalDays - totalDays*2
  }else{
    totalDays = totalDays - totalDays*2
  }
  return totalDays
}
